package com.tamartech.greendb.loader;

import java.util.Date;

import org.apache.log4j.Logger;

import com.tamartech.greendb.dao.ContactDAO;
import com.tamartech.greendb.dao.PersonDAO;
import com.tamartech.greendb.data.Contact;
import com.tamartech.greendb.data.Person;

public class CanvassLoader
{
    private static Logger log = Logger.getLogger (CanvassLoader.class);

    private Date canvassDate;
    private String contactedBy;
    
    public CanvassLoader()
    {
        
    }
    
    public void begin (Date canvassDate, String contactedBy) throws Exception
    {
        this.canvassDate = canvassDate;
        this.contactedBy = contactedBy; 
    }
    
    public void end()
    {
        
    }
    
    public void putContact (long personID, 
                            String party,
                            Integer rank,
                            String notes)
    {
        try
        {
            PersonDAO pDAO = new PersonDAO();
            
            Person p = pDAO.get(personID);
            
            if (p == null)
                throw new Exception ("Person " + personID + " not found");
            
            Contact c = new Contact();
            
            c.setDate(canvassDate);
            c.setContactedBy(contactedBy);
            c.setParty(party);
            c.setRank(rank);
            c.setNotes(notes);
            c.setPerson(p);
            c.setUpdatedOn(new Date());
            
            
            ContactDAO cDAO = new ContactDAO();
            cDAO.save(c);
        }
        catch (Exception e)
        {
            log.error("putContact. " + personID, e);
        }
    }
}
